package cn.adelyn.blog.file.controller;

import cn.adelyn.blog.file.service.FileService;
import cn.adelyn.common.core.execption.AdelynException;
import cn.hutool.core.io.IoUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import jakarta.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@RequestMapping("/vedio")
@RestController
public class VedioController {

    @Autowired
    FileService fileService;

    @GetMapping("/{vedioId}")
    public void getVedio(@PathVariable(value = "vedioId")
                         @NotNull(message = "vedioId 不能为空~") Long vedioId, HttpServletResponse httpServletResponse) {
        InputStream inputStream = fileService.getFile(
                "testvedio.mp4"
        );

        httpServletResponse.setContentType("video/mp4");
        try(OutputStream outputStream = httpServletResponse.getOutputStream()){
            IoUtil.copy(inputStream, outputStream);
        } catch (IOException e) {
            throw new AdelynException("下载图片失败: " + e.getMessage());
        }
    }
}
